/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;


    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import Model.Curve;
import Model.Shape;
import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author PhucAnh
 */
public class PlayBack extends javax.swing.JDialog {

    java.awt.Frame parent;
    LinkedList<Shape> objs ;
    public MyPanel1 jpnPlay ;
    public PlayBack(java.awt.Frame parent, boolean modal,LinkedList<Shape> myObjs) {
//        super(parent, modal);  
        setUndecorated(true);
        this.parent=parent;
        initComponents();
        objs = myObjs;
        this.setSize(1320,650);
        jpnPlay = new MyPanel1(myObjs);
        jpnPlay.setSize(1320, 550);
        jpnPlay.setBackground(Color.WHITE);
        
        this.add(jpnPlay);
        this.setLocationRelativeTo(null);
    }
    public void setOSC(Image OSC){
        jpnPlay.OSC1=OSC;
    }
    
    public class MyPanel1 extends Canvas{
    View view;
    Image OSC;
    Image OSC1;
    LinkedList<Object> ll;

   
   public MyPanel1(LinkedList ll1) {
      setBackground(Color.white);
      this.ll=ll1;
      
   }
   
 
   private void setupOSC() {
       BufferedImage bufferedImage = new BufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setComposite(AlphaComposite.Src);
        graphics2D.drawImage(OSC1, 0, 0,this.getWidth(),this.getHeight(), null);
        graphics2D.dispose();
        OSC=bufferedImage;
   }
   
   private Graphics getOSG() {
       setupOSC();
       return OSC.getGraphics();
   }
   
   public void clearOSC() {
       repaint();
   }

   public void update(Graphics g) {
      paint(g);
   }
   
   public void paint(Graphics g) {
      setupOSC();
      g.drawImage(OSC, 0, 0, this);
      for(int i =0;i<ll.size();i++)
            {
                Graphics g1=OSC.getGraphics();
                ((Shape)ll.get(i)).replay(g,50,OSC,this);
                ((Shape)ll.get(i)).drawShape(g1,(BufferedImage)OSC);
                try {
                                Thread.sleep(50);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(Curve.class.getName()).log(Level.SEVERE, null, ex);
                            }
            }
   }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

       btnReplay = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        this.addWindowListener( new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent we) {
                        parent.setVisible(true);
                    }
                } );
        btnReplay.setText("Replay");
        btnReplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReplayActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(btnReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(406, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(363, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReplay)
                    .addComponent(btnSave)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        this.dispose();
        this.parent.setVisible(true);
    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        
    }                
        private void btnReplayActionPerformed(java.awt.event.ActionEvent evt) {                                          
        jpnPlay.clearOSC();
        jpnPlay.repaint();
    }              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayBack.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayBack.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayBack.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayBack.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReplay;
    private javax.swing.JButton btnSave;
    // End of variables declaration                   
}


